package org.jeecg.modules.demo.ch.service.impl;

import org.jeecg.modules.demo.ch.entity.ChMaterialCodeAccessories;
import org.jeecg.modules.demo.ch.entity.ChPartsCodeComparison;
import org.jeecg.modules.demo.ch.mapper.ChPartsCodeComparisonMapper;
import org.jeecg.modules.demo.ch.mapper.ChMaterialCodeAccessoriesMapper;
import org.jeecg.modules.demo.ch.service.IChMaterialCodeAccessoriesService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 物料编码配件类表
 * @Author: jeecg-boot
 * @Date:   2022-09-16
 * @Version: V1.0
 */
@Service
public class ChMaterialCodeAccessoriesServiceImpl extends ServiceImpl<ChMaterialCodeAccessoriesMapper, ChMaterialCodeAccessories> implements IChMaterialCodeAccessoriesService {

	@Autowired
	private ChMaterialCodeAccessoriesMapper chMaterialCodeAccessoriesMapper;
	@Autowired
	private ChPartsCodeComparisonMapper chPartsCodeComparisonMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ChMaterialCodeAccessories chMaterialCodeAccessories, List<ChPartsCodeComparison> chPartsCodeComparisonList) {
		chMaterialCodeAccessoriesMapper.insert(chMaterialCodeAccessories);
		if(chPartsCodeComparisonList!=null && chPartsCodeComparisonList.size()>0) {
			for(ChPartsCodeComparison entity:chPartsCodeComparisonList) {
				//外键设置
				entity.setChMaterialCodeAccessoriesId(chMaterialCodeAccessories.getId());
				chPartsCodeComparisonMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ChMaterialCodeAccessories chMaterialCodeAccessories,List<ChPartsCodeComparison> chPartsCodeComparisonList) {
		chMaterialCodeAccessoriesMapper.updateById(chMaterialCodeAccessories);
		
		//1.先删除子表数据
		chPartsCodeComparisonMapper.deleteByMainId(chMaterialCodeAccessories.getId());
		
		//2.子表数据重新插入
		if(chPartsCodeComparisonList!=null && chPartsCodeComparisonList.size()>0) {
			for(ChPartsCodeComparison entity:chPartsCodeComparisonList) {
				//外键设置
				entity.setChMaterialCodeAccessoriesId(chMaterialCodeAccessories.getId());
				chPartsCodeComparisonMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		chPartsCodeComparisonMapper.deleteByMainId(id);
		chMaterialCodeAccessoriesMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			chPartsCodeComparisonMapper.deleteByMainId(id.toString());
			chMaterialCodeAccessoriesMapper.deleteById(id);
		}
	}
	
}
